package com.google.code.solrdimension.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.solrdimension.config.DimensionConfigWrapper;
import com.google.code.solrdimension.parsers.exception.SolrDimensionException;
import com.google.code.solrdimension.querymodifiers.QueryModifier;

public class DimensionQuery {
    
    private SolrQuery solrQuery;
    
    private QueryModifier queryModifier;
    
    private DimensionConfigWrapper solrDimensionConfig;
    
    private static Logger logger = LoggerFactory.getLogger(DimensionQuery.class);
    
    /**
     * must be a list of String in the format of "SolrfieldName:filtervalue"
     */
    private List<String> filtersToApply;
    
    
    
    
    public DimensionQuery(SolrQuery query, DimensionConfigWrapper solrDimensionconfig, QueryModifier queryModifier) throws SolrDimensionException{
        
        if (query == null || queryModifier == null || solrDimensionconfig == null){
            throw new SolrDimensionException("One or more of the required parameters is / are null.");
        }
        
        this.queryModifier = queryModifier;
        this.solrQuery = query;
        this.solrDimensionConfig = solrDimensionconfig;
        
        
        
    }
    
    public void setFiltersToApply(List<String> filtersToApply){
        
        this.filtersToApply = filtersToApply;
        
    }
    
    public void addFilterToApply(String filterToApply){
        
        if (filtersToApply == null){
            filtersToApply = new ArrayList<String>();
        }
        
        filtersToApply.add(filterToApply);
        
    }
    

    public SolrQuery getSolrQuery(){
        
        try {
            queryModifier.prepareQuery(solrQuery, solrDimensionConfig);
        } catch (SolrDimensionException e) {
            logger.error("Problem preparing the query...");
            e.printStackTrace();
        }
        
        return solrQuery;
    }

    

}
